package com.jhz.speedy.component.application;

import android.app.Application;
import android.content.IntentFilter;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;

import com.jhz.speedy.component.control.MyControl;
import com.jhz.speedy.component.receiver.NetStateReceiver;

/**
 * Application类
 * Created by JunHeZ on 2017/6/27.
 */
public class MyApplication extends Application {

    private static MyApplication application;
    private PackageInfo packageInfo;

    private NetStateReceiver netStateReceiver = new NetStateReceiver(); // 网络状态变更接收器


    @Override
    public void onCreate() {
        super.onCreate();
        application = this;
        registerReceiver(netStateReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));

        MyControl.getPlaySound().load(this);   // 预加载声音
    }



    /**
     * 获取实例
     * @return MyApplication
     */
    public static MyApplication getInstance() {
        if (null == application) {
            application = new MyApplication();
        }

        return application;
    }

    /**
     * 获取当前程序的版本信息
     */
    public PackageInfo getPackageInfo() {
        if (packageInfo == null) {
            // 获取包管理类
            PackageManager packageManager = getPackageManager();
            try {
                // getPackageName()是你当前类的包名，0代表是获取版本信息
                PackageInfo packInfo = packageManager.getPackageInfo(getPackageName(), 0);
                packageInfo = packInfo;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return packageInfo;
    }
}
